package com.paic.mhis.hcpms.common.baseFile.biz.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.common.util.Constants;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementConstants;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementSqlMapIdConstants;
import com.paic.mhis.hcpms.common.baseFile.biz.service.BaseFileService;
import com.paic.mhis.hcpms.common.baseFile.util.BaseFileServiceUtil;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
 * Description: 文件公共实现 hcpms_j2ee EX-LUORONG001 创建
 */
@Component("baseFileService")
public class BaseFileServiceImpl extends BaseService implements BaseFileService {
	@Autowired
	private CommonDAO commonDao;

	private BaseFileServiceUtil baseFileServiceUtil;
	private static final Logger logger = Logger.getLogger(BaseFileServiceImpl.class);

	/**
	 * 文件信息新增
	 */
	@Override
	@Transactional
	public Map<String, Object> addImage(JSONObject transferJson, String currentUser,String fileName,long fileSize)
			throws BusinessServiceException {
		try{
			baseFileServiceUtil = (BaseFileServiceUtil) context.getBean("baseFileServiceUtil");
		}catch(Exception e){
			logger.info(e);
		}
		return baseFileServiceUtil.addImage(transferJson, currentUser, fileName,fileSize);

	}

	/**
	 * 查询文件list
	 */
	@Override
	public Map<String, Object> getImageList(String id) throws BusinessServiceException {
		baseFileServiceUtil = (BaseFileServiceUtil) context.getBean("baseFileServiceUtil");
		return baseFileServiceUtil.getImage(id);

	}

	/**
	 * 文件删除
	 */
	@Override
	public Map<String, Object> deleteFile(JSONObject transferJson) throws BusinessServiceException {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			// 删除时，验证是否该协议下是否存在支付方式
			// 存在：提示是否继续删除；
			// id
			String id = transferJson.getString("id");
			paramsMap.put("id", id);
			// 是否有效[0-有效;1-无效]
			paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
			paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
			// 协议逻辑删除
			this.commonDao.update(AgreementSqlMapIdConstants.FILE_DELETE, paramsMap);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_AGREEMENT_INFO_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private void generateSuccessResult(Map<String, Object> result, String stateValue, String msgValue) {
		result.put(Constants.STATE_KEY, stateValue);
		result.put(Constants.MSG_KEY, msgValue);
	} 

}
